<template>
  <div class="container">
    <div class="left-bar">
      <div class="title">人事管理系统</div>
      <div class="menu">
        <div v-for="menu in menuList" :key="menu.index">
          <div class="menu-item" @click="openSubmenu(menu)">
            <span><i :class="menu.icon"></i> {{menu.name}}</span>
            <i :class="menu.show ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i>
          </div>
          <div v-if="menu.show" class="submenu-wrapper">
            <div v-for="(submenu, index) in menu.child" :key="index" class="submenu-item" :class="{'active': curMenuIdx===menu.index && curSubMenuIdx===index}" @click="changeMenu(menu.index, index)">{{submenu.name}}</div>
          </div>
        </div>
        <div class="menu-item" @click="$router.push({ name:  'help', params: {articleId: '1328252452340858882'}})"><span><i class="el-icon-document"></i> 使用说明</span></div>
      </div>
    </div>
    <div class="main-content">
      <router-view ref="component"></router-view>
    </div>
  </div>
</template>

<script>
import dingMixin from  '@/mixins/ding'
export default {
  data () {
    return {
      // menuList: [{index: 0, name: 'HR招聘', icon: 'el-icon-user', show: true, child: [{name: '招聘列表', url: 'recruitList'},{name: '招聘设置', url: 'recruitSetting'}]},
      //   {index: 1, name: '人事档案', icon: 'el-icon-s-data', show: false,  child: [{name: '在职员工花名册', url: 'employeeList'}, {name: '离职员工花名册', url: 'formerEmployee'}, {name: '个人信息', url: 'personalInformation'}, {name: '考勤列表', url: 'attendanceList'}]},
      //   {index: 2, name: '奖惩动态', icon: 'el-icon-sugar', show: false,  child: [{name: '积分红利', url: 'integralDividend'}, {name: '力沃奖惩', url: 'rewardsList'}, {name: '绩效列表', url: 'performanceList'}]},
      //   {index: 3, name: '固定资产', icon: 'el-icon-coin', show: false,  child: [{name: '资产列表', url: 'assetsList'}, {name: '资产设定', url: 'assetsSetting'}]},
      //   {index: 4, name: '系统设置', icon: 'el-icon-setting', show: false,  child: [{name: '权限设置', url: 'rightsSetting'}, {name: '操作日志', url: 'log'}, {name: '参数设置', url: 'argumentSetting'}]}],
      menuList: [],
      curMenuIdx: 0,
      curSubMenuIdx: 0,
      permissions: [],
    };
  },
  components: {},
  mixins: [dingMixin],
  watch: {
    $route(to, from) {
      if(to.name === 'administration' && (this.$route.query.type === '1' || this.$route.query.type === '2') && from.name === 'administration-recruitList') {
        let id = this.$route.query.id;
        let type = this.$route.query.type;
        this.$router.replace({ name: 'administration-recruitList'})
        if(type === '2') this.$refs.component.showDetailDialog(false, id)
      } else if(to.name === 'administration' && (this.$route.query.type === '1' || this.$route.query.type === '2')) {
        let id = this.$route.query.id;
        let type = this.$route.query.type;
        this.getCurMenuByUrl('administration-recruitList')
        this.$router.replace({ name: 'administration-recruitList', params: {id: id, type: type} })
      } else if(to.name === 'administration' && this.$route.query.type === '0' && from.name === 'administration-employeeList') {
        let id = this.$route.query.id;
        this.$router.replace({ name: 'administration-employeeList'})
        this.$refs.component.showDetailDialog(id)
      } else if(to.name === 'administration' && this.$route.query.type === '0') {
        let id = this.$route.query.id;
        this.getCurMenuByUrl('administration-employeeList')
        this.$router.replace({ name: 'administration-employeeList', params: {id: id} })
      }
    }
  },
  mounted () {
    this.getDingConfig().then(config =>{
      this.dingConfig = config;
      this.dingLogin(config.corpId, () => {
        this.getMenuNav();
      });
      this.run_dd_config();
    })
  },
  methods: {
    getCurMenuByUrl(url) {
      this.menuList.forEach( menu => { menu.show = false })
      this.menuList.forEach((menu, menuIdx) => {
        menu.child.forEach((subMenu, subMenuIdx) => {
          if('administration-'+subMenu.url === url) {
            menu.show = true
            this.curMenuIdx = menuIdx
            this.curSubMenuIdx = subMenuIdx
          }
        })
      })
    },
    openSubmenu(menu) {
      if(menu.show) {
        menu.show = false
      } else {
        this.menuList.forEach( menu => { menu.show = false })
        menu.show = true
      }
    },
    changeMenu(menuIdx, subMenuIdx) {
      this.curMenuIdx = menuIdx;
      this.curSubMenuIdx = subMenuIdx;
      this.$router.push({ name: 'administration-'+this.menuList[menuIdx].child[subMenuIdx].url })
    },
    getMenuNav() {
      this.$http({
        url: this.$http.adornUrl('hr/menu/nav'),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          sessionStorage.setItem('menuId', JSON.stringify(data.menuId || '[]'))
          sessionStorage.setItem('permissions', JSON.stringify(data.perm || '[]'))
          //根据权限展示菜单
          this.menuList = []
          if(data.perm.includes('recruitment:hrrecruitmentinterviewee:list')) this.menuList.push({index: this.menuList.length, name: 'HR招聘', icon: 'el-icon-user', show: true, child: [{name: '招聘列表', url: 'recruitList'}, {name: '招聘设置', url: 'recruitSetting'}]})
          if(data.perm.includes('sys:user:list')) {
            this.menuList.push({index: this.menuList.length, name: '人事档案', icon: 'el-icon-s-data', show: false,  child: [{name: '在职员工花名册', url: 'employeeList'}, {name: '离职员工花名册', url: 'formerEmployee'}, {name: '考勤列表', url: 'attendanceList'}]})
          } else {
            this.menuList.push({index: this.menuList.length, name: '人事档案', icon: 'el-icon-s-data', show: false,  child: [{name: '个人信息', url: 'personalInformation'}, {name: '考勤列表', url: 'attendanceList'}]})
          }
          this.menuList.push({index: this.menuList.length, name: '奖惩动态', icon: 'el-icon-sugar', show: false,  child: [{name: '积分红利', url: 'integralDividend'}, {name: '力沃奖惩', url: 'rewardsList'}, {name: '绩效列表', url: 'performanceList'}]})
          if(data.perm.includes('assets:hrassets:list')) this.menuList.push({index: this.menuList.length, name: '固定资产', icon: 'el-icon-coin', show: false,  child: [{name: '资产列表', url: 'assetsList'}, {name: '资产设定', url: 'assetsSetting'}]})

          if(data.perm.includes('hr:role:save') || data.perm.includes('hr:role:update') || data.perm.includes('hr:role:delete') || data.perm.includes('hr:roleuser:setting') ||
            data.perm.includes('hr:dict:save') || data.perm.includes('hr:dict:update') || data.perm.includes('hr:dict:delete') || 
            data.perm.includes('dynamic:hrdynamic:list')) {
            this.menuList.push({index: this.menuList.length, name: '系统设置', icon: 'el-icon-setting', show: false, child: []})
            if(data.perm.includes('hr:role:save') || data.perm.includes('hr:role:update') || data.perm.includes('hr:role:delete') || data.perm.includes('hr:roleuser:setting')) this.menuList[this.menuList.length-1].child.push({name: '权限设置', url: 'rightsSetting'})
            if(data.perm.includes('hr:dict:save') || data.perm.includes('hr:dict:update') || data.perm.includes('hr:dict:delete')) this.menuList[this.menuList.length-1].child.push({name: '参数设置', url: 'argumentSetting'})
            if(data.perm.includes('dynamic:hrdynamic:list')) this.menuList[this.menuList.length-1].child.push({name: '操作日志', url: 'log'})
          }
          //默认跳转页面
          let jumpRouteName = ''
          if(this.$route.query.type === '0' && data.perm.includes('sys:user:list')) {
            jumpRouteName = 'administration-employeeList'
          } else if (data.perm.includes('recruitment:hrrecruitmentinterviewee:list')) {
            jumpRouteName = 'administration-recruitList'
          } else {
            jumpRouteName = 'administration-'+this.menuList[0].child[0].url
          }
          this.getCurMenuByUrl(jumpRouteName)
          this.$router.replace({ name: jumpRouteName, params: { id: this.$route.query.id, type: this.$route.query.type } })
        } else {
          sessionStorage.setItem('menuId', '[]')
          sessionStorage.setItem('permissions', '[]')
          this.getCurMenuByUrl('administration-personalInformation')
          this.$router.replace({ name: 'administration-personalInformation' })
        }
      })
    }
  }
}
</script>

<style lang='scss' scoped>
.container {
  height: 100vh;
  width: 100%;
  background: #FFF;
  display: flex;
  .left-bar {
    width: 208px;
    min-width: 208px;
    height: 100%;
    background-color: #303334;
    color: #AEAEB0;
    border-right: 1px solid #DCDFE6;
    .title {
      text-align: center;
      height: 50px;
      line-height: 50px;
      margin-bottom: 24px;
      font-size: 16px;
      color: #FFF;
    }
    .menu {
      .menu-item {
        cursor: pointer;
        height: 44px;
        line-height: 44px;
        padding: 0 20px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        color: #FFF;
      }
      .submenu-item {
        cursor: pointer;
        height: 44px;
        line-height: 44px;
        padding: 0 20px;
        padding-left: 40px;
      }
      .active {
        background-color: #3C3F41;
        padding-left: 36px;
        border-left: 4px solid #1890FF;
        color: #FFF;
      }
    }
  }
  .main-content {
    flex: 1;
    width: 100%;
    overflow: auto;
  }
}
</style>